package readcf

import (
	"encoding/json"
	"flag"
	"fmt"
	"io/ioutil"
)

type ServerList struct {
	Host string `json:"Host"`
	Tag  string `json:"Tag"`
	Path string `json:"Path"`
}

type WsCfg struct {
	ServerList  map[string]ServerList `json:"ServerList"`
	DialTimeout int                   `json:"DialTimeout"`
	LogPath     string                `json:"LogPath"`
}

func ReadCf() (*WsCfg, error) {
	Conf := flag.String("config", "", "config file name")
	flag.Parse()
	fmt.Println("use config: ", *Conf)

	var wscfg WsCfg
	file, err := ioutil.ReadFile(*Conf)
	if err != nil {
		return nil, err
	}
	err = json.Unmarshal([]byte(file), &wscfg)
	if err != nil {
		return nil, err
	}

	return &wscfg, nil
}

func GetParm() *WsCfg {
	var Content, err = ReadCf()
	if err != nil {
		panic(err)
	}
	return Content
}
